package cn.hlife.com.hlfhaemocyte.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.hlife.com.hlfhaemocyte.R;


/**
 * Created by My on 2017/4/21.
 */

public class DialogUtils {

    public interface OnRetryListener {
        void retry();
    }

    ;

    public interface OnProgressView {
        void progressOnProgressView(ProgressBar progressBar);
    }

    public void showDialog(Context context, String title, String message, final OnRetryListener retry) {
        showDialog(context,title,message,"重试","放弃",retry);
    }
    public void showDialog(Context context, String title, String message, String submitMessage , String cancleMessage , final OnRetryListener retry) {
        final AlertDialog.Builder bulider = new AlertDialog.Builder(context);
        bulider.setPositiveButton(submitMessage, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                retry.retry();
            }
        });
        bulider.setNegativeButton(cancleMessage, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        bulider.setTitle(title);
        bulider.setMessage(message);
        Dialog dialog = bulider.create();
        dialog.show();
//        setDialogFullScreen(dialog);
    }
    public Dialog createLoadingDialog(Context context, String msg) {
       return  createLoadingDialog(context,msg,null);
    }
    public Dialog createLoadingDialog(Context context, String msg, OnProgressView onProgressView) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
        TextView tipTextView = (TextView) v.findViewById(R.id.tvMsg);// 提示文字
        ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progressbar);
        tipTextView.setText(msg);// 设置加载信息
        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);// 不可以用“返回键”取消
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        dialog.show();
        if (progressBar != null&&onProgressView!=null) {
            onProgressView.progressOnProgressView(progressBar);
        }
//        progressBar.setProgress();
//        setDialogFullScreen(dialog);
        return dialog;
    }
    public Dialog createLucencyLoadingDialog(Context context) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.lucency_dialog, null);// 得到加载view
        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);// 不可以用“返回键”取消
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        dialog.show();
        return dialog;
    }
    public void setLoadingDialogProgress(long pro) {


    }


    public static Dialog hintDialog(Context context, String msg) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.hint_dialog, null);// 得到加载view
        TextView tipTextView = (TextView) v.findViewById(R.id.tvMsg);// 提示文字
        tipTextView.setText(msg);// 设置加载信息
        final Dialog dialog = new Dialog(context, R.style.LoadingDialogStyle);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);// 不可以用“返回键”取消
        dialog.setContentView(v, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        dialog.show();
        return dialog;
    }

    /**
     * 设置Dialog全屏隐藏虚拟按键
     *
     * @param alertDialog
     */
    public void setDialogFullScreen(final Dialog alertDialog) {
        alertDialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        alertDialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                if (Build.VERSION.SDK_INT >= 19) {
                    uiOptions |= 0x00001000;
                } else {
                    uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
                }
                alertDialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
        });
    }
}
